<?php

// Created by Khanh Nam
class InvoiceequipmentController extends Controller{
    public $layout = '//layouts/column2';

    public function filters() {
        return array(
            'accessControl', // perform access control for CRUD operations
            array(
                'application.filters.RequireLoginFilter',
            ),
        );
    }

    /**
     * Specifies the access control rules.
     * This method is used by the 'accessControl' filter.
     * @return array access control rules
     */
    public function accessRules() {
        return array(
            array('allow', // allow all users to perform 'index' and 'view' actions

                'users' => array('@'),
            ),
            array('deny', // deny all users
                'users' => array('*'),
            ),
        );
    }
    public function actionUpdate(){
        if(isset($_POST['form']) && $_POST['form']=='invoiceequipment'){
            $model=NULL;
            if(isset($_POST['id']) && $_POST['id']!=0){
                $model=$this->loadModel($_POST['id']);
                $model->attributes=$_POST;
                
            }else{
                $model=new InvoiceEquipment;
                $model->attributes=$_POST;
            }
            if($model->save()){
                echo 'Save successful';
            }else{
                echo json_encode($model->getErrors());
            }
        }
    }
    public function actionDelete($id){
        $this->loadModel($id)->delete();
    }
    public function loadModel($id){
        $model=InvoiceEquipment::model()->findByPk($id);
        if($modell==NULL){
            throw new CHttpException("Your request can not process",404);
        }
        return $model;
    }
    public function actionList($invoice,$page=0,$sort=NULL){
        if ($page > 0)
            $page--;
        $order=CJSON::decode($sort);
        $items = new CActiveDataProvider('InvoiceEquipment', array(
                    'criteria' => array(
                        'condition' => "invoice=:invoice",
                        'params'=>array(
                            'invoice'=>$invoice
                        ),
                        'order'=>isset($sort)?$order[0]['property'].' '.$order[0]['direction']:NULL,
                    ),
                    'pagination' => array(
                        'pageSize' => 20,
                        'currentPage' => $page,
                    ),
                ));

        $itemsList = array();
        if ($items->getTotalItemCount() > 0) {
            $itemsList['totalCount'] = $items->getTotalItemCount();
            $itemsList['type'] = 'Items';
            foreach ($items->getData() as $i=>$item) {
                $itemsList['data'][$i] = $item->getAttributes();
         
            }
            echo json_encode($itemsList);
        }
        
    }
}
?>
